#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年9月24日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
import logging
sys.path.append(os.path.join(current_dir,'../'))

'''
    从hive按月统计 独立mac 非独立mac 数量

'''




logging.basicConfig(level=logging.DEBUG)

from logging.handlers import RotatingFileHandler

level = logging.DEBUG

Rthandler = RotatingFileHandler(os.path.join(current_dir,'log/pylog.log'), maxBytes=10*1024*1024,backupCount=0)
Rthandler.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Rthandler.setFormatter(formatter)
logging.getLogger('').addHandler(Rthandler)

console = logging.StreamHandler()
console.setLevel(level)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

from base.zhimind_hive_datas import myZhimindHiveDatas
from base.time_method import myTimeMethod




def main(st,et):
    pass
    if st==None or et==None:
        logging.error('st and et not be none.')
        sys.exit()
    days = myTimeMethod.get_slist_between_st_et(st, et)
    for day in days:
        if int(day.split('-')[-1])!=1:continue
        logging.info( 'start run %s'%day)
        
        # 每月非独立mac
        monthSt = day
        monthEt = re.sub('-01$','-31',day)
        
        monthVisitCount = myZhimindHiveDatas.get_flow(st=monthSt, et=monthEt,distinct=False)
        monthCustomerVisitCount = myZhimindHiveDatas.get_flow(st=monthSt, et=monthEt,role = 3,distinct=False)
        
        # 每月独立mac
        monthMacCount = myZhimindHiveDatas.get_flow(st=monthSt, et=monthEt,distinct=True)
        monthCustomerMacCount = myZhimindHiveDatas.get_flow(st=monthSt, et=monthEt,distinct=True,role=3)
        #monthMacCount = 0
        #monthCustomerMacCount = 0
        
        # 独立mac总数 自2014-09-01 起
        totalMacCount = myZhimindHiveDatas.get_flow( st= '2014-09-01' ,et=monthEt,distinct=True)
        totalCustomerMacCount = myZhimindHiveDatas.get_flow(st='2014-09-01', et=monthEt,distinct=True,role=3)
        #totalMacCount = 0
        #totalCustomerMacCount = 0
        
        print monthVisitCount,monthMacCount,totalMacCount
        
        print ','.join(['day','monthVisitCount','monthMacCount','totalMacCount','type'])
        print ','.join([day,str(monthVisitCount),str(monthMacCount),str(totalMacCount),'all'])
        print ','.join([day,str(monthCustomerVisitCount),str(monthCustomerMacCount),str(totalCustomerMacCount),'customer'])
        logging.info(','.join(['day','monthVisitCount','monthMacCount','totalMacCount','type']))
        logging.info(','.join([day,str(monthVisitCount),str(monthMacCount),str(totalMacCount),'all']))
        logging.info(','.join([day,str(monthCustomerVisitCount),str(monthCustomerMacCount),str(totalCustomerMacCount),'customer']))


import argparse,re,datetime,time
parser = argparse.ArgumentParser(description='args')
parser.add_argument('--startTime',metavar=u"starttime",default=None)
parser.add_argument('--endTime',metavar=u"endtime",default=None)

args = parser.parse_args()
startTime = args.startTime 
endTime = args.endTime

main(startTime,endTime)

